﻿using System;
using System.Linq;
using Algoritmia.DataStructures.Lists;
using Algoritmia.Utils;

namespace Algoritmia.Problems.Geometry.ClosestPair
{
    public class BruteForceClosestPointsFinder : IClosestPointsFinder
    {
        #region Implementation of IClosestPointsFinder

        public double DistanceBetweenClosestPoints(IList<Point2D> points)
        {
            return (from i in Enumerable.Range(0, points.Count)
                    from j in Enumerable.Range(i + 1, points.Count-i-1)
                    select Utils.EuclideanDistance(points[i], points[j])).Min();
        }

        public Tuple<Point2D, Point2D> ClosestPoints(IList<Point2D> points)
        {
            return (from i in Enumerable.Range(0,points.Count)
                    from j in Enumerable.Range(i+1,points.Count-i-1)
                    select new Tuple<Point2D, Point2D>(points[i], points[j])).ArgMin(
                        t => Utils.EuclideanDistance(t.Item1, t.Item2));
        }

        #endregion
    }
}
